﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.ServiceReference1;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Sysbase
{
    public class MenuController : BaseController
    {
        // GET: Sysbase/Menu
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            string name = WebUtils.GetString("name");
            string menuId = WebUtils.GetString("menuId");
            string menuType = WebUtils.GetString("menuType");
            ResultHelperOfDataTable serviceResult = service.GetMenuDt(name, menuId, menuType);
            DataTable dt = new DataTable();
            if (serviceResult.S)
            {
                dt = serviceResult.D;
                if (dt != null)
                {
                    dt.DefaultView.Sort = "sort";
                    dt = dt.DefaultView.ToTable();
                }
            }

            var result = new { Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            tb_Menu menu = new tb_Menu();
            if (id > 0)
            {
                FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
                ResultHelperOftb_Menu result = service.Detailmenu(id);
                if (result.S)
                {
                    menu = result.D;
                }
            }
            ViewBag.Menu = menu;
            return View();
        }

        /// <summary>
        /// 添加/编辑菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(tb_Menu menu)
        {
            OperateResult resultInfo = MenuManager.Save(menu);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 获取上级菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult GetParentMenu()
        {
            DataTable dt = new DataTable();
            FunctionAndPermissionsSoapClient service = new FunctionAndPermissionsSoapClient();
            ResultHelperOfDataTable result = service.getmenutreelist();
            if (result.S)
            {
                dt = result.D;
            }
            return JsonDate(dt);
        }

        /// <summary>
        /// 获取指定图标
        /// </summary>
        /// <returns></returns>
        public ActionResult GetIcons()
        {
            string rootPath = HttpContext.Server.MapPath("~/Content/images/newIcon/");
            return Content(MenuManager.GetAllFilesNameInDirectory(rootPath));
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = MenuManager.Delete(id);
            return JsonDate(resultInfo);
        }
    }
}